/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entities;

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.MapsId;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author Alejandro
 */
@Entity
@Table(name = "RS_ITEM_PURCHASE")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "ItemPurchased.findAll", query = "SELECT r FROM ItemPurchased r"),
    @NamedQuery(name = "ItemPurchased.findByItem", query = "SELECT r FROM ItemPurchased r WHERE r.primaryKey = :primaryKey"),
    @NamedQuery(name = "ItemPurchased.findByQuantity", query = "SELECT r FROM ItemPurchased r WHERE r.quantity = :quantity"),
    @NamedQuery(name = "ItemPurchased.findByPrice", query = "SELECT r FROM ItemPurchased r WHERE r.price = :price")})
public class ItemPurchased implements Serializable {

    private static final long serialVersionUID = 1L;
//
//    @Id
//    @ManyToOne
//    @JoinColumn(name="ID_ITEM", nullable = false)
//    private Item item;  
//    @Id
//    @ManyToOne
//    @JoinColumn(name="ID_PURCHASE", nullable = false)
//    private Purchase purchase;
    @Id
    @Basic(optional = false)
    @NotNull
    @GeneratedValue
    @Column(name = "ID_ITEM_PURCHASE")
    private Integer primaryKey;
  
    @ManyToOne
    private Purchase purchase;
    @ManyToOne
    private Item item;
    
    @Column(name = "QUANTITY")
    private Integer quantity;
    @Column(name = "PRICE")
    private Double price;

    public ItemPurchased() {
    }



    public ItemPurchased(Item item,Purchase purchase, Integer quantity, Double price) {
        this.primaryKey = new Integer(purchase.getIdPurchase()+item.getIdItem().toString());//Esto puede provocar un desbordamiento en la BD, o no
        this.purchase=purchase;
        this.item=item;
        
        this.quantity = quantity;
        this.price = price;
    }

    public Integer getPrimaryKey() {
        return primaryKey;
    }

    public void setPrimaryKey(Integer primaryKey) {
        this.primaryKey = primaryKey;
    }



    public Purchase getPurchase() {
        return purchase;
    }

    public void setPurchase(Purchase purchase) {
        this.purchase = purchase;
    }

    public Item getItem() {
        return item;
    }

    public void setItem(Item item) {
        this.item = item;
    }

//    public Purchase getPurchase() {
//        return purchase;
//    }
//
//    public void setPurchase(Purchase purchase) {
//        this.purchase = purchase;
//    }
//
//   
//    public Item getItem() {
//        return item;
//    }
//
//    public void setItem(Item item) {
//        this.item = item;
//    }
    public Integer getQuantity() {
        return quantity;
    }

    public void setQuantity(Integer quantity) {
        this.quantity = quantity;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        //  hash += (purchase != null ? purchase.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof ItemPurchased)) {
            return false;
        }
        ItemPurchased other = (ItemPurchased) object;
//        if ((this.purchase == null && other.purchase != null) || (this.purchase != null && !this.purchase.equals(other.purchase))) {
//            return false;
//        }
        return true;
    }
    
    public Double resumen(){
      return Math.rint((quantity*price)*100)/100;

    }

    @Override
    public String toString() {
        return "entities.ItemPurchase[ idItemPurchase="+ purchase + " ]";
    }
}
